<?php
class ControllerAuthorization extends ControllerMain implements iController
{
	protected $user; //object of user
	protected $login; //request login
	protected $password; //request password
	
	protected function setMainTemplate()
	{
		$this->mainTemplate = $this->templateDir.'authorization.html';
	}

	public function index()
	{
		if(isset($_SESSION['AUTH']) && $_SESSION['AUTH'] == true)
		{
			header("Location:index.php");	
		}
		else
		{
			$this->login();
			$this->viewForm();
		}	
	}

	protected function callHeader()
	{
		//Empty header
	}

	protected function callSidebar()
	{
		//Empty sidebar
	}
	
	/*
	*Administrator log out action
	*/
	public function logout()
	{
		$this->user = new User();
		$this->user->logout();
		$this->index();
	}
	
	/*
	*Administrator log in action
	*/
	protected function login()
	{
		$this->keys['message'] = '';
		$this->keys['login'] = '';
		if(isset($this->post['login']) && isset($this->post['password']) && isset($_POST['loginSubmit']))
		{
			if(!empty($this->post['login']) && !empty($this->post['password']) && $_POST['loginSubmit'] == 'Login')
			{
				$this->login = $this->post['login'];
				$this->password = $this->post['password'];
				$this->user = new User();
				if($this->user->login($this->login,$this->password))
				{
					header("Location:index.php");
				}
				else
				{
					$this->keys['login'] = $this->login;
					$this->keys['message'] = 'Incorrect data for authorization';	
				}	
			}
			elseif(!empty($this->post['login']))
			{
				$this->keys['login'] = $this->post['login'];
				$this->keys['message'] = 'You don\'t enter all information';
			}
			else
			{
				$this->keys['message'] = 'You don\'t enter all information';	
			}	
		}		
	}
	
	/*
	*View form for admin-authorization
	*/
	protected function viewForm()
	{	
		$this->template = View::create($this->templateDir.'viewForm.html',$this->keys);
		View::assign('content',$this->template);
	}
}
